using System.Collections;
using System.Collections.Generic;
using System.IO;
using Common;
using Common.Enum;
using Common.NotifyEvent;

namespace SSettings
{
	public class MemorySettings : NotifyPropertyChanged
	{
		private EProjectType _Type;

		private bool _UseHotBonnet = true;

		private ETemperatureControlMode _TemperatureControlMode;

		private EScanMode _ScanMode;

		private string _ExperimentExportPath = Utility.GetMyDocumentFolder();

		private string _ExperimentExportExt = "txt";

		private Hashtable _htStandardCurver = new Hashtable();

		private int _AutoIndexInterval = 1;

		private int _AutoIndexFormatLength = 4;

		public EProjectType Type
		{
			get
			{
				return _Type;
			}
			set
			{
				if (_Type != value)
				{
					_Type = value;
					OnPropertyChanged("Type");
				}
			}
		}

		public bool UseHotBonnet
		{
			get
			{
				return _UseHotBonnet;
			}
			set
			{
				if (_UseHotBonnet != value)
				{
					_UseHotBonnet = value;
					OnPropertyChanged("UseHotBonnet");
				}
			}
		}

		public ETemperatureControlMode TemperatureControlMode
		{
			get
			{
				return _TemperatureControlMode;
			}
			set
			{
				if (_TemperatureControlMode != value)
				{
					_TemperatureControlMode = value;
					OnPropertyChanged("TemperatureControlMode");
				}
			}
		}

		public EScanMode ScanMode
		{
			get
			{
				return _ScanMode;
			}
			set
			{
				if (_ScanMode != value)
				{
					_ScanMode = value;
					OnPropertyChanged("ScanMode");
				}
			}
		}

		public string ExperimentExportPath
		{
			get
			{
				return _ExperimentExportPath;
			}
			set
			{
				if (!(_ExperimentExportPath == value))
				{
					_ExperimentExportPath = value;
					OnPropertyChanged("ExperimentExportPath");
				}
			}
		}

		public string ExperimentExportExt
		{
			get
			{
				return _ExperimentExportExt;
			}
			set
			{
				if (!(_ExperimentExportExt == value))
				{
					_ExperimentExportExt = value;
					OnPropertyChanged("ExperimentExportExt");
				}
			}
		}

		public Hashtable htStandardCurver
		{
			get
			{
				return _htStandardCurver;
			}
		}

		public int AutoIndexInterval
		{
			get
			{
				return _AutoIndexInterval;
			}
			set
			{
				_AutoIndexInterval = value;
			}
		}

		public int AutoIndexFormatLength
		{
			get
			{
				return _AutoIndexFormatLength;
			}
			set
			{
				_AutoIndexFormatLength = value;
			}
		}

		public string GetStandardCurver(string key)
		{
			if (htStandardCurver.Contains(key))
			{
				return htStandardCurver[key] as string;
			}
			return null;
		}

		public void AddStandardCurver(string key, string value)
		{
			if (htStandardCurver.Contains(key))
			{
				htStandardCurver[key] = value;
			}
			else
			{
				htStandardCurver.Add(key, value);
			}
		}

		public void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_MemorySettings);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			list.Add(PropertyKey.Key_Type);
			list2.Add(BytesConverter.GetBytes((int)Type));
			list.Add(PropertyKey.Key_UseHotBonnet);
			list2.Add(BytesConverter.GetBytes(UseHotBonnet));
			list.Add(PropertyKey.Key_TemperatureControlMode);
			list2.Add(BytesConverter.GetBytes((int)TemperatureControlMode));
			list.Add(PropertyKey.Key_ScanMode);
			list2.Add(BytesConverter.GetBytes((int)ScanMode));
			list.Add(PropertyKey.Key_AutoIndexInterval);
			list2.Add(BytesConverter.GetBytes(AutoIndexInterval));
			list.Add(PropertyKey.Key_AutoIndexFormatLength);
			list2.Add(BytesConverter.GetBytes(AutoIndexFormatLength));
			if (!string.IsNullOrEmpty(ExperimentExportPath))
			{
				list.Add(PropertyKey.Key_ExperimentExportPath);
				list2.Add(BytesConverter.GetBytes(ExperimentExportPath));
			}
			if (!string.IsNullOrEmpty(ExperimentExportExt))
			{
				list.Add(PropertyKey.Key_ExperimentExportExt);
				list2.Add(BytesConverter.GetBytes(ExperimentExportExt));
			}
			if (htStandardCurver.Count > 0)
			{
				list.Add(PropertyKey.Key_StandardCurver);
				StreamEx streamEx = new StreamEx();
				streamEx.Write(BytesConverter.GetBytes(htStandardCurver.Count));
				foreach (string key in htStandardCurver.Keys)
				{
					streamEx.Write(key);
					streamEx.Write(htStandardCurver[key] as string);
				}
				streamEx.Seek(0L, SeekOrigin.Begin);
				list2.Add(streamEx.ToArray());
				streamEx.Close();
			}
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public bool ReadFromStream(StreamEx stream)
		{
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey != PropertyKey.Key_MemorySettings)
			{
				stream.Seek(-4L, SeekOrigin.Current);
				return false;
			}
			propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_Index)
			{
				List<PropertyKey> keys;
				List<byte[]> dates;
				stream.ReadIndex(out keys, out dates);
				bool flag = true;
				for (int i = 0; i < keys.Count; i++)
				{
					if (!flag)
					{
						return false;
					}
					switch (keys[i])
					{
					case PropertyKey.Key_Type:
						Type = (EProjectType)BytesConverter.BytesToInt(dates[i]);
						break;
					case PropertyKey.Key_UseHotBonnet:
						UseHotBonnet = BytesConverter.BytesToBool(dates[i]);
						break;
					case PropertyKey.Key_TemperatureControlMode:
						TemperatureControlMode = (ETemperatureControlMode)BytesConverter.BytesToInt(dates[i]);
						break;
					case PropertyKey.Key_ScanMode:
						ScanMode = (EScanMode)BytesConverter.BytesToInt(dates[i]);
						break;
					case PropertyKey.Key_ExperimentExportPath:
						ExperimentExportPath = BytesConverter.BytesToString(dates[i]);
						break;
					case PropertyKey.Key_ExperimentExportExt:
						ExperimentExportExt = BytesConverter.BytesToString(dates[i]);
						break;
					case PropertyKey.Key_StandardCurver:
					{
						htStandardCurver.Clear();
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						int num = streamEx.ReadInt();
						for (int j = 0; j < num; j++)
						{
							htStandardCurver.Add(streamEx.ReadString(), streamEx.ReadString());
						}
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_AutoIndexInterval:
						AutoIndexInterval = BytesConverter.BytesToInt(dates[i]);
						break;
					case PropertyKey.Key_AutoIndexFormatLength:
						AutoIndexFormatLength = BytesConverter.BytesToInt(dates[i]);
						break;
					}
				}
				return true;
			}
			stream.Seek(-4L, SeekOrigin.Current);
			while (true)
			{
				switch (stream.ReadKey())
				{
				case PropertyKey.Key_Type:
					Type = (EProjectType)stream.ReadInt();
					break;
				case PropertyKey.Key_UseHotBonnet:
					UseHotBonnet = stream.ReadBool();
					break;
				case PropertyKey.Key_TemperatureControlMode:
					TemperatureControlMode = (ETemperatureControlMode)stream.ReadInt();
					break;
				case PropertyKey.Key_ScanMode:
					ScanMode = (EScanMode)stream.ReadInt();
					break;
				case PropertyKey.Key_ExperimentExportPath:
					ExperimentExportPath = stream.ReadString();
					break;
				case PropertyKey.Key_ExperimentExportExt:
					ExperimentExportExt = stream.ReadString();
					break;
				case PropertyKey.Key_StandardCurver:
				{
					int num2 = stream.ReadInt();
					htStandardCurver.Clear();
					for (int k = 0; k < num2; k++)
					{
						htStandardCurver.Add(stream.ReadString(), stream.ReadString());
					}
					break;
				}
				default:
					stream.Seek(-4L, SeekOrigin.Current);
					return true;
				}
			}
		}
	}
}
